package com.fosung.frame.tsdemo.front.web;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fosung.frame.common.mapper.JsonMapper;
import com.fosung.frame.common.utils.StringUtils;
import com.fosung.frame.modules.sys.service.DictService;
import com.fosung.frame.modules.sys.utils.DictUtils;
import com.fosung.frame.tsdemo.front.service.PartyBuildingNewService;

@Controller
@RequestMapping("/newsController")
public class PartyBuildingNewsController {

	@Resource
	private PartyBuildingNewService service;
	@Autowired
	private DictService dictService;
	
	
	public String getIp(){
		return dictService.get("d99234504b8e47788280caa36831b770").getValue();
	}
	/**
	 * 查询消息详情信息
	 * @param id
	 * @return
	 * 
	 */
	@ResponseBody
	@RequestMapping("/getNoticeById")
	public String getNoticeById(String id,HttpServletResponse response){
		response.setHeader("Access-Control-Allow-Origin", "*");
		Map<String, Object> map = service.getNoticeById(id);
		String img = (String) map.get("image");
		String content = (String) map.get("content");
		if(!StringUtils.isEmpty(img)){
			map.put("image", getIp() + img.substring(img.indexOf("/userfiles")));
		}
		if(!StringUtils.isEmpty(content)){
			if(content.indexOf("src")>0){
				content = content.replace("/userfiles", getIp()+"/userfiles");				
				map.put("content", content);
			}
		}
		return JsonMapper.toJsonString(map);
	}
	
	/**
	 * 获取模块列表
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getModuleList")
	public String getModuleList(String userId,HttpServletResponse response){
		response.setHeader("Access-Control-Allow-Origin", "*");
		Map<String,Object> result=new HashMap<String,Object>();
		List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
		list =service.getModuleGroup(userId);
		for(int i=0;i<list.size();i++){
			Map<String,Object> map = list.get(i);
			String groupId= (String)map.get("officeGroup");
			String lable =DictUtils.getDictLabel(groupId, "group_view", "");
			Map<String,Object> m=new HashMap<String,Object>();
			m.put("title", lable);
			List<Map<String,Object>> list1 = service.getModuleLists(userId, groupId);
			for(int j=0;j<list1.size();j++){
				Map<String,Object> mj = list1.get(j);
				String img = (String) mj.get("img");
				if(!StringUtils.isEmpty(img)){
					mj.put("img", getIp() + img.substring(img.indexOf("/userfiles")));
				}
			}
			m.put("list", list1);
			result.put("m"+i, m);
		}
		return JsonMapper.toJsonString(result);
	}
	
	/**
	 * 党建热点活动详情信息
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getMeetingById")
	public String getMeetingById(String id,HttpServletResponse response){
		response.setHeader("Access-Control-Allow-Origin", "*");
		Map<String, Object> map = service.getMeetingById(id);
		String image = (String) map.get("image");
		String content = (String) map.get("content");
		if(!StringUtils.isEmpty(image)){
			map.put("image", getIp() + image.substring(image.indexOf("/userfiles")));
		}
		if(!StringUtils.isEmpty(content)){
			if(content.indexOf("src")>0){
				content = content.replace("/userfiles", getIp()+"/userfiles");
				map.put("content", content);
			}
		}
		return JsonMapper.toJsonString(map);
	}
	
	/**
	 * 获取学习列表
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getLearnList")
	public String getNoticeLearnList(String userId,HttpServletResponse response){
		response.setHeader("Access-Control-Allow-Origin", "*");
		List<Map<String,Object>> list = service.getLearnList(userId);
		for(int i=0;i<list.size();i++){
			Map<String,Object> map = list.get(i);
			String content = (String) map.get("content");
			String image = (String) map.get("image");
			if(!StringUtils.isEmpty(image)){
				map.put("image", getIp() + image.substring(image.indexOf("/userfiles")));
			}
			if(content.indexOf("<img") != -1){
				content = content.substring(0,content.indexOf("<img")) + content.substring(content.lastIndexOf("/>")+2);
				map.put("content", content);
			}
		}
		return JsonMapper.toJsonString(list);
	}
	
	/**
	 * 获取消息列表
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getNoticeList")
	public String getNoticeList(String userId,HttpServletResponse response){
		response.setHeader("Access-Control-Allow-Origin", "*");
		List<Map<String,Object>> list = service.getNoticeList(userId);
		List<Map<String,Object>> days = service.getNoticeDays(userId);
		
		for(int i=0;i<list.size();i++){
			Map<String,Object> map = list.get(i);
			String image = (String) map.get("image");
			String content = (String) map.get("content");
			if(!StringUtils.isEmpty(image)){
				map.put("image", getIp() + image.substring(image.indexOf("/userfiles")));
			}
			if(content.indexOf("<img") != -1){
				content = content.substring(0,content.indexOf("<img")) + content.substring(content.lastIndexOf("/>")+2);
				map.put("content", content);
			}
		}
		
		List<Map<String,Object>> newList = new ArrayList<Map<String,Object>>();
		for(int i=0;i<days.size();i++){
			String day = (String) days.get(i).get("create_date");
			List<Map<String,Object>> l = new ArrayList<Map<String,Object>>();
			for(int j=0;j<list.size();j++){
				if(day.equals(list.get(j).get("create_date"))){
					l.add(list.get(j));
				}
			}
			Map<String,Object> m = new HashMap<String,Object>();
			m.put("date", day);
			m.put("list",l);
			newList.add(m);
		}
		return JsonMapper.toJsonString(newList);
	}
	
}
